/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministratorFrm.java
 *
 * Created on May 3, 2010, 3:16:47 PM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ManagementBiz;
import com.aptech.campaign.component.ColumnData;
import com.aptech.campaign.component.GUIProperties;
import com.aptech.campaign.component.ObjectTableModel;
import com.aptech.campaign.entities.Account;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Tai Nguyen
 */
public class AdministratorFrm extends javax.swing.JFrame {

    public ManagementBiz managementBuz = new ManagementBiz();
    public ObjectTableModel tableModel;
    public ArrayList<Account> arr = null;
    public JTable headerTable;

    //
    public AdministratorFrm() {
        arr = new ArrayList<Account>();

        initComponents();
        //managementBuz = new ManagementBiz();
        loadTable();


        try {
            setIconImage(new ImageIcon(getClass().getResource("../icon/symbol.png")).getImage());
            setLocationRelativeTo(null);
            GUIProperties prop = new GUIProperties();
            UIManager.setLookAndFeel(prop.getLookAndFeel());
            SwingUtilities.updateComponentTreeUI(this);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AdministratorFrm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AdministratorFrm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AdministratorFrm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(AdministratorFrm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * load Account to table
     */
    public void loadTable() {
        arr = managementBuz.findByAllAccount();
        ColumnData[] columns = {
            new ColumnData("UserName", 100, SwingConstants.LEFT, 1),
            new ColumnData("FullName", 100, SwingConstants.LEFT, 3),
            new ColumnData("Birthday", 100, SwingConstants.LEFT, 4),
            new ColumnData("Phone", 100, SwingConstants.LEFT, 5),
            new ColumnData("Gender", 100, SwingConstants.LEFT, 6)
        };

        tableModel = new ObjectTableModel(tbAccount, columns, arr);
        headerTable = tableModel.getHeaderTable();

        //Auto set ordinal
        headerTable.createDefaultColumnsFromModel();
        tbAccount.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();

        //display data with ordinal
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scrAccount.setRowHeader(viewport);
        scrAccount.setPreferredSize(new Dimension(550, 300));
        scrAccount.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable.getTableHeader());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scrAccount = new javax.swing.JScrollPane();
        tbAccount = new javax.swing.JTable();
        pnlAccMana = new javax.swing.JPanel();
        lblAccMana = new javax.swing.JLabel();
        pnlEdit = new javax.swing.JPanel();
        btnChangePermission = new javax.swing.JButton();
        btnNewAccount = new javax.swing.JButton();
        btnEditAcc = new javax.swing.JButton();
        pnlListAcc = new javax.swing.JPanel();
        lblAccList = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Administrator");
        setMinimumSize(new java.awt.Dimension(800, 400));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        tbAccount.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        scrAccount.setViewportView(tbAccount);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.6;
        getContentPane().add(scrAccount, gridBagConstraints);

        pnlAccMana.setBackground(new java.awt.Color(51, 102, 255));
        pnlAccMana.setMinimumSize(new java.awt.Dimension(798, 50));
        pnlAccMana.setPreferredSize(new java.awt.Dimension(11, 50));
        pnlAccMana.setLayout(new java.awt.BorderLayout());

        lblAccMana.setBackground(new java.awt.Color(51, 102, 255));
        lblAccMana.setFont(new java.awt.Font("Tahoma", 1, 30)); // NOI18N
        lblAccMana.setForeground(new java.awt.Color(51, 102, 255));
        lblAccMana.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/ACCOUNTMANAGEMENTBANNER.jpg"))); // NOI18N
        pnlAccMana.add(lblAccMana, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.2;
        getContentPane().add(pnlAccMana, gridBagConstraints);

        pnlEdit.setMinimumSize(new java.awt.Dimension(9, 43));
        pnlEdit.setPreferredSize(new java.awt.Dimension(42, 30));
        pnlEdit.setLayout(new java.awt.GridBagLayout());

        btnChangePermission.setBackground(new java.awt.Color(51, 102, 255));
        btnChangePermission.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnChangePermission.setForeground(new java.awt.Color(255, 255, 255));
        btnChangePermission.setText("Change Permission");
        btnChangePermission.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnChangePermission.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePermissionActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        pnlEdit.add(btnChangePermission, gridBagConstraints);

        btnNewAccount.setBackground(new java.awt.Color(51, 102, 255));
        btnNewAccount.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnNewAccount.setForeground(new java.awt.Color(255, 255, 255));
        btnNewAccount.setText("New Account");
        btnNewAccount.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnNewAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewAccountActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        pnlEdit.add(btnNewAccount, gridBagConstraints);

        btnEditAcc.setBackground(new java.awt.Color(51, 102, 255));
        btnEditAcc.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnEditAcc.setForeground(new java.awt.Color(255, 255, 255));
        btnEditAcc.setText("Edit Account");
        btnEditAcc.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnEditAcc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditAccActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        pnlEdit.add(btnEditAcc, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.7;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(pnlEdit, gridBagConstraints);

        pnlListAcc.setPreferredSize(new java.awt.Dimension(40, 37));

        lblAccList.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblAccList.setForeground(new java.awt.Color(51, 102, 255));
        lblAccList.setText("ACCOUNT LIST");

        javax.swing.GroupLayout pnlListAccLayout = new javax.swing.GroupLayout(pnlListAcc);
        pnlListAcc.setLayout(pnlListAccLayout);
        pnlListAccLayout.setHorizontalGroup(
            pnlListAccLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 798, Short.MAX_VALUE)
            .addGroup(pnlListAccLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlListAccLayout.createSequentialGroup()
                    .addGap(0, 332, Short.MAX_VALUE)
                    .addComponent(lblAccList)
                    .addGap(0, 332, Short.MAX_VALUE)))
        );
        pnlListAccLayout.setVerticalGroup(
            pnlListAccLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 42, Short.MAX_VALUE)
            .addGroup(pnlListAccLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlListAccLayout.createSequentialGroup()
                    .addGap(0, 10, Short.MAX_VALUE)
                    .addComponent(lblAccList)
                    .addGap(0, 10, Short.MAX_VALUE)))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        getContentPane().add(pnlListAcc, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNewAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewAccountActionPerformed
        CreateAccountDlg add = new CreateAccountDlg(this, true);
        add.setSize(400, 500);
        add.setVisible(true);
    }//GEN-LAST:event_btnNewAccountActionPerformed

    private void btnEditAccActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditAccActionPerformed
        EditAccountDlg edit = new EditAccountDlg(this, true);
        edit.setSize(392, 400);
        //
        arr = managementBuz.findByAllAccount();
        //get userName of the row is selected on table
        int indexRow = tbAccount.getSelectedRow();
        //set info on fields of EditAccountDlg
        edit.txfUserName.setText(arr.get(indexRow).getUserName());
        edit.txfFullName.setText(arr.get(indexRow).getFullName());
        edit.txfPhone.setText(arr.get(indexRow).getFone());
        edit.dtcBirthday.setDate(arr.get(indexRow).getBirthday());
        String gender = arr.get(indexRow).getGender();
        if (gender.equalsIgnoreCase("Female")) {
            edit.rdbFemale.setSelected(true);

        }
        edit.setVisible(true);
    }//GEN-LAST:event_btnEditAccActionPerformed

    private void btnChangePermissionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePermissionActionPerformed
        //
        arr = managementBuz.findByAllAccount();
        //get userName of the row is selected on table
        int indexRow = tbAccount.getSelectedRow();
        if (arr.get(indexRow).getUserName().compareTo("tai_adm01") == 0) {
            JOptionPane.showMessageDialog(this, "Can not change permission of this Account!");
        } else {
            ChangePermissionDlg permis = new ChangePermissionDlg(this, true);
            permis.setSize(350, 400);

            //set text on lblUser of ChangePermissionDlg
            permis.lblUser.setText(arr.get(indexRow).getUserName());
            //get PermissionName of user is selected
            String permissionName = managementBuz.findPermissionName(permis.lblUser.getText());
            permis.lblPresentPermiss.setText(permissionName);
            permis.setVisible(true);
        }
    }//GEN-LAST:event_btnChangePermissionActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AdministratorFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChangePermission;
    private javax.swing.JButton btnEditAcc;
    private javax.swing.JButton btnNewAccount;
    private javax.swing.JLabel lblAccList;
    private javax.swing.JLabel lblAccMana;
    private javax.swing.JPanel pnlAccMana;
    private javax.swing.JPanel pnlEdit;
    private javax.swing.JPanel pnlListAcc;
    private javax.swing.JScrollPane scrAccount;
    public javax.swing.JTable tbAccount;
    // End of variables declaration//GEN-END:variables
}
